package com.hjs.pacloudservice.controller;

import com.hjs.pacloudservice.remote.FileUploadRemote;
import com.hjs.pacloudservice.service.IFileUploadService;
import com.hjs.pacloudservice.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.hjs.pacloudservice.pojo.File;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.net.MalformedURLException;


/**
 * @auther hanjinsong
 * @create 2019-08-21 11:28
 */
@Slf4j
@Controller
public class FileController {

    @Autowired
    private IFileUploadService iFileService;

    @Autowired
    private RedisService redisService;

    @GetMapping("/")
    public String listUploadedFiles(Model model) throws IOException {
        iFileService.init();//初始化
        model.addAttribute("files",redisService.selectAllValues());
        return "index";
    }

    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) throws MalformedURLException {

        Resource file = iFileService.loadAsResource(filename);
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + file.getFilename() + "\"").body(file);
    }

    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) throws IOException {

        long startTime=System.currentTimeMillis();//获取开始时间
        iFileService.store(file);
        long endTime=System.currentTimeMillis(); //获取结束时间
        log.info("上传时间： "+(endTime-startTime)+"ms");
        redirectAttributes.addFlashAttribute("message",
                "You successfully uploaded " + file.getOriginalFilename() + "! 上传使用了"+(endTime-startTime)+"ms");

        return "redirect:/";
    }


//    @Autowired
//    private FileUploadRemote fileUploadRemote;
//    //@GetMapping("/selectById")
//    // RESTful
//    @RequestMapping(value = "/files", method = RequestMethod.GET)
//    public File selectById(){
//        System.out.println("service----------");
//        return fileUploadRemote.selectById(9);
//    }


//    //这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
//    @Autowired
//    private RestTemplate restTemplate;
//
//    @GetMapping("/")
//    public ResponseEntity<File> selectById(){
//
//        //restTemplate.getForObject("http://pacloud-dao", File.class);
//        ResponseEntity<File> entity = restTemplate.getForEntity("http://pacloud-dao", File.class);
//
//
//        return entity;
//    }

}
